//
//  WTEmoticonPageView.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/7/4.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit

// cell可重用标识符
private let reusedId = "PageViewCell"

class WTEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setUpUI(){
    
    backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
     // 设置代理
    dataSource = self
      // 注册cell
    registerClass(WTEmoticonPageViewCell.self, forCellWithReuseIdentifier: reusedId)
    // 设置layout
     let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
      // 设置itemSize
     flowLayout.itemSize = CGSize(width: kScreenWidth, height: 216 - 37)
      // 设置间距
     flowLayout.minimumInteritemSpacing = 0
     flowLayout.minimumLineSpacing = 0
      // 设置滚动方向
     flowLayout.scrollDirection = .Horizontal
        
//        bounces = false;
//        pagingEnabled = true
     
    }

}

extension WTEmoticonPageView:UICollectionViewDataSource{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
       
        return WTEmoticonTools.sharedEmoticonTools.allEmoticons.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WTEmoticonTools.sharedEmoticonTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reusedId, forIndexPath: indexPath) as! WTEmoticonPageViewCell
        // cell 设置随机颜色
//        cell.backgroundColor = RandomColor()
        
        //赋值indexPath
        cell.indexpath = indexPath
        
        // 给cell 赋值
        cell.emoticons = WTEmoticonTools.sharedEmoticonTools.allEmoticons[indexPath.section][indexPath.item]
        
        cell.backgroundColor = self.backgroundColor
        
        return cell
    }
    

}